$(function(){
	var $tabList=$("#tab_list"),$pagebar=$("#pagebar ul"),$pageInfo=$("#page_info"),
		pageNo=1,pageSize=10,count=0;//初始值
	/**---------------封装分页方法,count:总条目数,pageNo:当前页---------------*/
	function pageit(count){
		$pagebar.empty();
		//首页和上一页
		if(pageNo==1){
			$pagebar.append("<li id='start_page' class='disabled'><a href='javascript:void(0);'>首页</a></li>");
			$pagebar.append("<li id='prev_page'  class='disabled'><a href='javascript:void(0);'>上一页</a></li>");
		}else{
			$pagebar.append("<li id='start_page' pn=1><a href='javascript:void(0);'>首页</a></li>");
			$pagebar.append("<li id='prev_page' pn="+(pageNo-1)+"><a href='javascript:void(0);'>上一页</a></li>");
		}
		var tmp=parseInt(count/10);
		var page_size=count%10==0?(tmp):(tmp+1);//总页数
		//中间数字页
		if(page_size<=5){
			//如果小于5页,则全部显示
			for(var i = 0; i <page_size; i++) {
				if(i==pageNo-1){
					$pagebar.append("<li pn="+(i+1)+" class='active'><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
				}else{
					$pagebar.append("<li pn="+(i+1)+"><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
				}
			}
		}else{
			//如果总页数大于5页,显示当前页和前后各两页以及两个...符号
			if(pageNo<3){
				for(var i = 0; i <5; i++) {
					if(i==pageNo-1){
						$pagebar.append("<li pn="+(i+1)+" class='active'><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
					}else{
						$pagebar.append("<li pn="+(i+1)+"><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
					}
				}
				$pagebar.append("<li class='disabled'><a href='javascript:void(0);'>...</a></li>");
			}else if(pageNo>page_size-2){
				$pagebar.append("<li class='disabled'><a href='javascript:void(0);'>...</a></li>");
				for(var i = page_size-5; i <page_size; i++) {
					if(i==pageNo-1){
						$pagebar.append("<li pn="+(i+1)+" class='active'><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
					}else{
						$pagebar.append("<li pn="+(i+1)+"><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
					}
				}
			}else{
				for (var i = pageNo-3; i <pageNo+2; i++) {
					if(i==pageNo-1){
						$pagebar.append("<li pn="+(pageNo)+" class='active'><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
					}else{
						$pagebar.append("<li pn="+(i+1)+"><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
					}
				}
			}
		}
		//下一页和末页
		if(page_size==0||pageNo==page_size){
			$pagebar.append("<li id='next_page' class='disabled'><a href='javascript:void(0);'>下一页</a></li>");
			$pagebar.append("<li id='end_page' class='disabled'><a href='javascript:void(0);'>末页</a></li>");
		}else{
			$pagebar.append("<li id='next_page' pn="+(pageNo+1)+"><a href='javascript:void(0);'>下一页</a></li>");
			$pagebar.append("<li id='end_page' pn="+page_size+"><a href='javascript:void(0);'>末页</a></li>");
		}
		
		//绑定事件
		$pagebar.find("li").not(".disabled").on("click",function(){
			pageNo=parseInt($(this).attr("pn"));//更新当前页码
			reloadList();
		});
		//分页信息条
		$pageInfo.find("span").each(function(i,k){
			if(i==0){
				if(page_size==0){
					$(k).html(0);
				}else{
					$(k).html((pageNo-1)*10+1);
				}
			}else if(i==1){
				$(k).html(count>pageNo*10?pageNo*10:count);
			}else{
				$(k).html(count);
			}
		});
	}
	/**------------------加载列表---------------------*/
	var params={"pageNo":1,"pageSize":pageSize,"name":null,"phone":null,"isDown":null,"status":0};
	function reloadList(){
		params.pageNo=pageNo;
		params.pageSize=pageSize;
		params.name=$("#search_name").val();
		params.phone=$("#search_phone").val();
		$.get(serverURL+"doctor/list",params,function(data){
//			data=$.parseJSON(data);
			try {
				if(data.code==0){
					data=data.data;
					$tabList.find("tbody").empty();
					count=data.pagination.totalCount;
					if(count>0){
						var html="";
						$.each(data.list,function(i,k){
							html+="<tr>" +
							"<td id='pkc'><input type='checkbox' name = "+k.isDown+" id='"+k.id+"' class='checkbox' col='"+i+"'></td>" +
							"<td>"+((pageNo-1)*10+i+1)+"</td>" +
							"<td>"+k.realName+"</td>"+ 
							"<td>"+(k.gender==0?"女":"男")+"</td>"+ 
							"<td>"+k.duty+"</td>"+ 
							"<td>"+k.department+"</td>"+ 
							"<td>"+k.hospital+"</td>"+ 
							"<td>"+k.phone+"</td>"+ 
							"<td>"+k.birthday+"</td>"+ 
							"<td>"+k.coin+"</td>"+ 
							"<td>"+(k.lastLoginTime?k.lastLoginTime:"无")+"</td>"+ 
							"<td>"+k.registerTime+"</td>"+ 
							"<td>"+(k.isDown==0?"<font  style='color:green;font-weight:bold;font-size:13px;'>正常</font>":"<font  style='color:red;font-weight:bold;font-size:13px;'>已下架</font>")+"</td>"+ 
							"</tr>";
						});
						$tabList.find("tbody").append(html);
						$tabList.find("tr").dblclick(function(){
							showScanModal($(this).find("#pkc input").attr("id"));
						});
//						$(".tools button").removeAttr("disabled");
					}else{
						$tabList.find("tbody").append("<tr><td colspan="+$("thead th").length+">无数据</td></tr>");
//						$(".tools button").not(".btn-default").attr("disabled","disabled");
					}
					pageit(count);//重绘工具栏
				}else if(data.code!=-1){
					alert("数据加载出错，请稍后重试");
				}
			} catch (e) {
				alert("数据加载出错，请稍后重试"+e);
			}
		});
	}
	reloadList();
	//全选与取消全选
	$("#pkall").change(function(){
		if($(this).prop("checked")){
			$tabList.find("#pkc :checkbox").prop("checked",true);
		}else{
			$tabList.find("#pkc :checkbox").prop("checked",false);
		}
	});
	//获取选中的行
	function getSelectedCols(){
		return $tabList.find("#pkc :checkbox").filter(":checked");
	}
	function getSelectIds(){
		var ids="",$selects=getSelectedCols();
		$selects.each(function(i,k){
			ids+=k.id+",";
		});
		ids=ids.substring(0, ids.length-1);
		return ids;
	}
	/**---------------查询-----------------*/
	$("#btn_search").click(function(){
		params.name=$("#search_name").val();
		params.phone=$("#search_phone").val();
		params.isDown=$("#search_is_down").val();
		pageNo=1;
		reloadList();
		return false;
	});
	/**---------------重置-----------------*/
	$("#btn_reset").click(function(){
		$("#form_search").get(0).reset();
		pageNo=1;
		params.isDown = null;
		reloadList();
		return false;
	});
	/**---------------审核按钮的状态过滤-----------------*/
	$("#btn_status button").click(function(){
		$(this).addClass("active").siblings().removeClass("active");
		params.status=$(this).attr("id");
		params.pageNo=1;
		if($(this).attr("id")==0){
			$("#btn_check button").removeAttr("disabled");
			$("#btn_down button").attr("disabled","disabled");
		}else{
			$("#btn_check button").attr("disabled","disabled");
			$("#btn_down button").removeAttr("disabled");
		}
		if($(this).attr("id")==1){
			$("#btn_down").removeAttr("disabled");
			$("#btn_edit").removeAttr("disabled");
			$("#btn_census").removeAttr("disabled");
		}else{
			$("#btn_edit").prop("disabled","disabled");
			$("#btn_census").prop("disabled","disabled");
			$("#btn_down").prop("disabled","disabled");
		}
		reloadList();
	});
	/**---------------审核-----------------*/
	$("#btn_check li").click(function(){
		if(params.status!=0){
			alert("该记录不需要审核");
			return;
		}
		var $selects=getSelectedCols();
		if($selects.length==0){
			alert("请选择一条数据进行审核");
			return;
		}
		if($(this).index()==1){
			$("#modal_reason").modal("show");
			return;
		}
		sendCheckRequest(getSelectIds(),$(this).index(),null);
	});
	function sendCheckRequest(ids,action,reason){
		$.post(serverURL+"doctor/check",{"ids":ids,"action":action,"reason":reason},function(data){
			if(data.code==0){
				alert("审核成功！");
				reloadList();
			}else if(data.code!=-1){
				alert("审核失败，请稍后重试");
			}
		});
	}
	var $reason=$("#form_reason").find("textarea");
	$("#btn_confirm_reject").click(function(){
		if($reason.val()){
			sendCheckRequest(getSelectIds(), 1, $reason.val());
		}else{
			alert("请输入驳回理由!");
			return false;
		}
	});
	/**---------------查看详情-----------------*/
	function showScanModal(id){
		var index=parseInt(params.status)+1,$form_scan=$("#form_scan"+index);
		$form_scan.get(0).reset();
		$.get(serverURL+"doctor/get?id="+id,function(data){
			if(data.code==0){
				$("#modal_scan"+index).modal("show");
				data=data.data;
				for(var key in data){
					$form_scan.find("[name="+key+"]").val((data[key]!=null)?data[key]:"无");
				}
				$form_scan.find("[name=gender]").val(data.gender==0?"女":"男");
				var $imgs=$form_scan.find("img");
				$imgs.eq(0).attr("src",data.halfPicUrl);
				if(data.cardUrl==null||data.cardUrl==""){
					$imgs.eq(1).hide().siblings("span").show();
				}else{
					$imgs.eq(1).show().attr("src",data.cardUrl).siblings("span").hide();
				}
				if(data.workProofPicUrl==null||data.workProofPicUrl==""){
					$imgs.eq(2).hide().siblings("span").show();
				}else{
					$imgs.eq(2).show().attr("src",data.workProofPicUrl).siblings("span").hide();
				}
//				$imgs.eq(1).attr("src",data.cardUrl);
//				$imgs.eq(2).attr("src",data.workProofPicUrl);
			}else if(data.code!=-1){
				alert("数据加载失败，请重试");
			}
		});
	}
	$("#btn_scan").click(function(){
		var $selects=getSelectedCols();
		if($selects.length!=1){
			alert("请选择一条数据查看,双击记录行也可查看");
			return;
		}
		showScanModal($selects.get(0).id);
	});
	
	/**-------------修改医生信息-----------------*/
	var $formEdit=$("#form_edit"),$editId=$formEdit.find("[name=id]"),
		$editName=$formEdit.find("[name=realName]"),$editGender=$formEdit.find("[name=gender]"),$editPhone=$formEdit.find("[name=phone]"),
		$editHospital=$formEdit.find("[name=hospital]"),$editDepartment=$formEdit.find("[name=department]"),$editDuty=$formEdit.find("[name=duty]"),
		$editBirthday=$formEdit.find("[name=birthday]"),$editWorkYear=$formEdit.find("[name=workStartYear]"),$editAskPrice=$formEdit.find("[name=askPrice]"),
		$editGoodAt=$formEdit.find("[name=goodAt]"),
		$logo2=$("#form_edit2 input:file"),$logoScan2=$("#logo_scan2"),
		$idcard2=$("#form_edit3 input:file"),$idcardScan2=$("#idcard_scan2"),
		$certificate2=$("#form_edit4 input:file"),$certificateScan2=$("#certificate_scan2");
	function scanLogo2(url){
		$logoScan2.parent().parent().parent().show();
		$logoScan2.attr("src",url).parent("a").attr("href",url);
	}
	function scanIdCard2(url){
		$idcardScan2.parent().parent().parent().show();
		$idcardScan2.attr("src",url).parent("a").attr("href",url);
	}
	function scanCertificate2(url){
		$certificateScan2.parent().parent().parent().show();
		$certificateScan2.attr("src",url).parent("a").attr("href",url);
	}
	function hideLogo2(){
		$logoScan2.attr("src","").parent("a").attr("href","");;
		$logoScan2.parent().parent().parent().hide();
	}
	function hideIdCard2(){
		$idcardScan2.attr("src","").parent("a").attr("href","");;
		$idcardScan2.parent().parent().parent().hide();
	}
	function hideCertificate2(){
		$certificateScan2.attr("src","").parent("a").attr("href","");;
		$certificateScan2.parent().parent().parent().hide();
	}
	
	//上传头像
	var imgReg=/^.+\.(jpg|png|bmp|gif)$/;
	$logo2.change(function(){
		if(!imgReg.test($logo2.val())){
			warnInput($logo2);
			return false;
		}
		$("#form_edit2").ajaxSubmit({  
			dataType:'json',
			resetForm:true,
	        success:function(data){
	        	if(data.code==0){
	        		scanLogo2(data.data);
	        	}else if(data.code!=-1){
	        		alert("头像上传失败，请重试");
	        		$("#form_edit2").get(0).reset();
	        	}
	        } 
		});
	});
	//上传身份证照
	var imgReg=/^.+\.(jpg|png|bmp|gif)$/;
	$idcard2.change(function(){
		if(!imgReg.test($idcard2.val())){
			warnInput($idcard2);
			return false;
		}
		$("#form_edit3").ajaxSubmit({  
			dataType:'json',
			resetForm:true,
	        success:function(data){
	        	if(data.code==0){
	        		scanIdCard2(data.data);
	        	}else if(data.code!=-1){
	        		alert("身份证照上传失败，请重试");
	        		$("#form_edit3").get(0).reset();
	        	}
	        } 
		});
	});
	//上传工作证照
	var imgReg=/^.+\.(jpg|png|bmp|gif)$/;
	$certificate2.change(function(){
		if(!imgReg.test($certificate2.val())){
			warnInput($certificate2);
			return false;
		}
		$("#form_edit4").ajaxSubmit({  
			dataType:'json',
			resetForm:true,
	        success:function(data){
	        	if(data.code==0){
	        		scanCertificate2(data.data);
	        	}else if(data.code!=-1){
	        		alert("工作证照上传失败，请重试");
	        		$("#form_edit4").get(0).reset();
	        	}
	        } 
		});
	});

    function openEditModal(id){
    	$formEdit.get(0).reset();
    	//给隐藏域id赋值
    	$editId.val(id);
    	//从后台获取该条记录所有信息
    	$.get(serverURL+"doctor/get?id="+id,function(data){
    		if(data.code==0){
    			data=data.data;
    			$editName.val(data.realName);
    			$editGender.val(data.gender);
    			$editPhone.val(data.phone);
    			data.hospitalId?$editHospital.val(data.hospitalId):null;
    			data.depId?$editDepartment.val(data.depId):null;
    			data.dutyId?$editDuty.val(data.dutyId):null;
    			$editBirthday.val(data.birthday);
    			$editWorkYear.val(data.workStartYear);
    			$editAskPrice.val(data.askPrice);
    			$editGoodAt.val(data.goodAt);
    			
    			scanLogo2(data.halfPicUrl);
    			if(!data.cardUrl==null&&!data.cardUrl==""){
    				scanIdCard2(data.cardUrl);
    			}
    			if(!data.workProofPicUrl==null&&!data.workProofPicUrl==""){
    				scanCertificate2(data.workProofPicUrl);
    			}
    			$("#modal_edit").modal("show");
    		}else if(data.code!=-1){
    			alert("医生信息加载失败");
    		}
    	});	
    }
    $("#btn_edit").click(function(){
    	//获取选中的行		
    	var $curCol=getSelectedCols();
    	if($curCol.length!=1){
    		alert("请选择一条记录修改");
    		return;
    	}
    	var id=$curCol[0].id;
    	openEditModal(id);
    });
    
 	//表单验证
	function checkEditForm(){
		if(!$editName.val()||$editName.val().length>10){
			warnInput($editName);
			return false;
		} 
		if(!$editPhone.val()||!(/^\d{11}$/.test($editPhone.val()))){
			warnInput($editPhone);
			return false;
		} 
		if($editHospital.val()==0){
			warnInput($editHospital);
			return false;
		} 
		if($editDepartment.val()==0){
			warnInput($editDepartment);
			return false;
		} 
		if($editDuty.val()==0){
			warnInput($editDuty);
			return false;
		} 
		if(!$editBirthday.val()){
			warnInput($editBirthday);
			return false;
		} 
		if(!$editWorkYear.val()||!(/^\d+$/.test($editWorkYear.val()))||$editWorkYear.val()<0){
			warnInput($editWorkYear);
			return false;
		}
		if(!$editAskPrice.val()||!(/^\d+$/.test($editAskPrice.val()))||$editAskPrice.val()<0){
			warnInput($editAskPrice);
			return false;
		}
		if(!$editGoodAt.val()||$editGoodAt.val().length>80){
			warnInput($editGoodAt);
			return false;
		} 
		if(!$logoScan2.attr("src")){
			warnInput($logo2);
			return false;
		} 
//    	if(!$idcardScan2.attr("src")){
//			warnInput($idcard2);
//			return false;
//		} 
//		if(!$certificateScan2.attr("src")){
//			warnInput($certificate2);
//			return false;
//		} 
		return true;
	}

	$("#birthday,#addbirthday,#registerTime").datetimepicker({
		format: "yyyy-mm-dd",
		language:"zh-CN",
		startView:4,
		minView:2,
		autoclose: true,
        todayBtn: true,
        pickDate: true,
        pickTime: false,
        todayHighlight:true
    });
	
    $("#btn_confirm_edit").click(function(){
    	//表单验证
    	if(checkEditForm()){
			$.post(serverURL+"doctor/modify",{"id":$editId.val(),"realName":$editName.val(),"phone":$editPhone.val(),"dutyId":$editDuty.val(),
				"depId":$editDepartment.val(),"hospitalId":$editHospital.val(),
				"gender":$editGender.val(),'birthday':$editBirthday.val(),"workStartYear":$editWorkYear.val(),
				"askPrice":$editAskPrice.val(),"goodAt":$editGoodAt.val(),
				"halfPicUrl":$logoScan2.attr("src"),"cardUrl":$idcardScan2.attr("src"),"workProofPicUrl":$certificateScan2.attr("src")},function(data){
				if(data.code==0){
					alert("修改成功");
					$('#modal_edit').modal('hide');//隐藏模态框
					$formEdit.get(0).reset();
					reloadList();
				}else if(data.code!=-1){
					alert("修改失败，请稍后重试");
				}
			});
		}
    });
	
	/**---------------修改获取职务列表-----------------*/
    $.get(serverURL+"duty/all",function(data){
    	if(data.code==0){
	    	var html="";
	    	$.each(data.data,function(i,k){
	    		html+="<option value='"+k.id+"'>"+k.name+"</option>";
	    	});
	    	$editDuty.append(html);
	    }else if(data.code!=-1){
			alert("职务数据加载失败");
		}
    });
    /**---------------修改获取医院列表-----------------*/
    $.get(serverURL+"hospital/all",function(data){
    	if(data.code==0){
	    	var html2="";
	    	$.each(data.data,function(m,n){
	    		html2+="<option value='"+n.id+"'>"+n.name+"</option>";
	    	});
	    	$editHospital.append(html2);
    	}else if(data.code!=-1){
			alert("医院数据加载失败");
		}
    });
    /**---------------修改获取科室列表-----------------*/
    $.get(serverURL+"department/all",function(data){
    	if(data.code==0){
	    	var html3="";
	    	$.each(data.data,function(a,b){
	    		html3+="<option value='"+b.id+"'>"+b.name+"</option>";
	    	});
	    	$editDepartment.append(html3);
    	}else if(data.code!=-1){
			alert("科室数据加载失败");
		}
    });
    
	/**---------------新增医生-----------------*/
	var $formAdd=$("#form_add"),
		$addName=$formAdd.find("[name=realName]"),$addGender=$formAdd.find("[name=gender]"),$addPhone=$formAdd.find("[name=phone]"),
		$addHospital=$formAdd.find("[name=hospital]"),$addDepartment=$formAdd.find("[name=department]"),$addDuty=$formAdd.find("[name=duty]"),
		$addBirthday=$formAdd.find("[name=birthday]"),$addWorkYear=$formAdd.find("[name=workStartYear]"),$addAskPrice=$formAdd.find("[name=askPrice]"),
		$addGoodAt=$formAdd.find("[name=goodAt]"),
		$logo=$("#form_add2 input:file"),$logoScan=$("#logo_scan"),
		$idcard=$("#form_add3 input:file"),$idcardScan=$("#idcard_scan"),
		$certificate=$("#form_add4 input:file"),$certificateScan=$("#certificate_scan");
    function scanLogo(url){
		$logoScan.parent().parent().parent().show();
		$logoScan.attr("src",url).parent("a").attr("href",url);
	}
    function scanIdCard(url){
		$idcardScan.parent().parent().parent().show();
		$idcardScan.attr("src",url).parent("a").attr("href",url);
	}
	function scanCertificate(url){
		$certificateScan.parent().parent().parent().show();
		$certificateScan.attr("src",url).parent("a").attr("href",url);
	}
	function hideIdCard(){
		$idcardScan.attr("src","").parent("a").attr("href","");;
		$idcardScan.parent().parent().parent().hide();
	}
	function hideCertificate(){
		$certificateScan.attr("src","").parent("a").attr("href","");;
		$certificateScan.parent().parent().parent().hide();
	}
	function hideLogo(){
		$logoScan.attr("src","").parent("a").attr("href","");;
		$logoScan.parent().parent().parent().hide();
	}

	//上传头像
	var imgReg=/^.+\.(jpg|png|bmp|gif)$/;
	$logo.change(function(){
		if(!imgReg.test($logo.val())){
			warnInput($logo);
			return false;
		}
		$("#form_add2").ajaxSubmit({  
			dataType:'json',
			resetForm:true,
	        success:function(data){
	        	if(data.code==0){
	        		scanLogo(data.data);
	        	}else if(data.code!=-1){
	        		alert("头像上传失败，请重试");
	        		$("#form_add2").get(0).reset();
	        	}
	        } 
		});
	});
    //上传身份证照
	var imgReg=/^.+\.(jpg|png|bmp|gif)$/;
	$idcard.change(function(){
		if(!imgReg.test($idcard.val())){
			warnInput($idcard);
			return false;
		}
		$("#form_add3").ajaxSubmit({  
			dataType:'json',
			resetForm:true,
	        success:function(data){
	        	if(data.code==0){
	        		scanIdCard(data.data);
	        	}else if(data.code!=-1){
	        		alert("身份证照上传失败，请重试");
	        		$("#form_add3").get(0).reset();
	        	}
	        } 
		});
	});
	//上传工作证照
	var imgReg=/^.+\.(jpg|png|bmp|gif)$/;
	$certificate.change(function(){
		if(!imgReg.test($certificate.val())){
			warnInput($certificate);
			return false;
		}
		$("#form_add4").ajaxSubmit({  
			dataType:'json',
			resetForm:true,
	        success:function(data){
	        	if(data.code==0){
	        		scanCertificate(data.data);
	        	}else if(data.code!=-1){
	        		alert("工作证照上传失败，请重试");
	        		$("#form_add4").get(0).reset();
	        	}
	        } 
		});
	});
 	//表单验证
	function checkAddForm(){
		if(!$addName.val()||$addName.val().length>10){
			warnInput($addName);
			return false;
		} 
		if(!$addPhone.val()||!(/^\d{11}$/.test($addPhone.val()))){
			warnInput($addPhone);
			return false;
		} 
		if($addHospital.val()==0){
			warnInput($addHospital);
			return false;
		} 
		if($addDepartment.val()==0){
			warnInput($addDepartment);
			return false;
		} 
		if($addDuty.val()==0){
			warnInput($addDuty);
			return false;
		} 
		if(!$addBirthday.val()){
			warnInput($addBirthday);
			return false;
		} 
		if(!$addWorkYear.val()||!(/^\d+$/.test($addWorkYear.val()))||$addWorkYear.val()<0){
			warnInput($addWorkYear);
			return false;
		}
		if(!$addAskPrice.val()||!(/^\d+$/.test($addAskPrice.val()))||$addAskPrice.val()<0){
			warnInput($addAskPrice);
			return false;
		}
		if(!$addGoodAt.val()||$addGoodAt.val().length>80){
			warnInput($addGoodAt);
			return false;
		} 
		if(!$logoScan.attr("src")){
			warnInput($logo);
			return false;
		} 
//    	if(!$idcardScan.attr("src")){
//			warnInput($idcard);
//			return false;
//		} 
//		if(!$certificateScan.attr("src")){
//			warnInput($certificate);
//			return false;
//		} 
		return true;
	}

	$("#birthday,#addbirthday,#registerTime").datetimepicker({
		format: "yyyy-mm-dd",
		language:"zh-CN",
		startView:4,
		minView:2,
		autoclose: true,
        todayBtn: true,
        pickDate: true,
        pickTime: false,
        todayHighlight:true
    });
	
	$("#btn_confirm_add").click(function(){
		if(checkAddForm()){
			$.post(serverURL+"doctor/add",{"realName":$addName.val(),"phone":$addPhone.val(),"dutyId":$addDuty.val(),
				"depId":$addDepartment.val(),"hospitalId":$addHospital.val(),
				"gender":$addGender.val(),'birthday':$addBirthday.val(),"workStartYear":$addWorkYear.val(),
				"askPrice":$addAskPrice.val(),"goodAt":$addGoodAt.val(),
				"halfPicUrl":$logoScan.attr("src"),"cardUrl":$idcardScan.attr("src"),"workProofPicUrl":$certificateScan.attr("src")},function(data){
				if(data.code==0){
					alert("添加成功");
					hideLogo();
					hideIdCard();
					hideCertificate();
					$("#modal_add").modal("hide");
					$formAdd.get(0).reset();
					reloadList();
				}else if(data.code!=-1){
					alert(data.msg);
				}
			});
		}
	});
	/**---------------新增获取职务列表-----------------*/
    $.get(serverURL+"duty/all",function(data){
    	if(data.code==0){
	    	var html="";
	    	$.each(data.data,function(i,k){
	    		html+="<option value='"+k.id+"'>"+k.name+"</option>";
	    	});
	    	$addDuty.append(html);
	    }else if(data.code!=-1){
			alert("职务数据加载失败");
		}
    });
    /**---------------新增获取医院列表-----------------*/
    $.get(serverURL+"hospital/all",function(data){
    	if(data.code==0){
	    	var html2="";
	    	$.each(data.data,function(m,n){
	    		html2+="<option value='"+n.id+"'>"+n.name+"</option>";
	    	});
	    	$addHospital.append(html2);
    	}else if(data.code!=-1){
			alert("医院数据加载失败");
		}
    });
    /**---------------新增获取科室列表-----------------*/
    $.get(serverURL+"department/all",function(data){
    	if(data.code==0){
	    	var html3="";
	    	$.each(data.data,function(a,b){
	    		html3+="<option value='"+b.id+"'>"+b.name+"</option>";
	    	});
	    	$addDepartment.append(html3);
    	}else if(data.code!=-1){
			alert("科室数据加载失败");
		}
    });
    /**-----------------统计信息--------------------*/
    var $formCensus=$("#form_census");
    $("#btn_census").click(function(){
//    	if(params.status!=1){
//    		alert("只有已审核通过的医生帐号才能查看统计信息");
//    		return;
//    	}
    	//获取选中的行		
    	var $curCol=getSelectedCols();
    	if($curCol.length!=1){
    		alert("请选择一条记录查看统计信息");
    		return;
    	}
    	var id=$curCol[0].id;
    	$.get(serverURL+"doctor/getTotalCensus?docId="+id,function(data){
    		if(data.code==0){
    			data=data.data;
    			$("#modal_census").modal("show");
    			$.each(data,function(i,k){
    				$formCensus.find("[name="+i+"]").val(k);
    			});
//    			$formCensus.find("[name=cashoutTotalCoin]").val(data.cashoutTotalAmount);
    			var rate=data.consultedTimes==0?0:(Math.round(data.scoredTimes*100/(data.consultedTimes))+"%");
    			$formCensus.find("[name=scoreRate]").val(rate);
    		}else if(data.code!=-1){
    			alert("统计信息加载失败");
    		}
    	});
    });
    
    /**
     * 医生账号状态
     */
    
	$("#btn_down li").click(function(){
    	var $curCol=getSelectedCols();
    	if($curCol == null||$curCol.length!=1){
    		alert("请选择一条医生记录下架");
    		return;
    	}
    	var id=$curCol[0].id;
    	var isDown=$curCol[0].name;
    	if($(this).index() ==0){//上架
    		if(isDown == 0){
    			alert("该医生已是上架");
    		}else{
    			doctorDownAndUp(id,0);  			
    		}
    	}else{// 下架
    		if(isDown == 1){
    			alert("该医生已是下架");
    		}else{
    			doctorDownAndUp(id,1); 
    		}
    	}
	});
	
	function doctorDownAndUp(id,isDown){	
		$.get(serverURL+"doctor/dactorDown",{"docId":id,"isDown":isDown},function(data){
			if(data.code==0){
				alert("该医生账号状态修改成功");
				reloadList();
			}else if(data.code!=-1){
				alert("该医生账号状态修改失败");
			}
		});    		
	}
	
});